<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<div style="padding-top:5px;">
	<a class='myBtn roundBorder' onclick="addField()">添加实验场地</a>
	<table class="commonTable" id='fieldTable'>
		<thead>
			<tr>
				<th>校区</th><th>楼号</th><th>楼层</th><th>房间号码</th><th>操作</th>
			</tr>
		</thead> 
		<tbody>
	 		<c:forEach items="${staffList}" var="field">
	 			<tr id="labfield${field.id}">
	 				<td>${field.room.floor.buildingInfo.schoolArea.schoolName }</td>
	 				<td>${field.room.floor.buildingInfo.buildingNumber}</td>
	 				<td>${field.room.floor.floorNumber }</td>
	 				<td>${field.room.roomNumber }</td>
	 				<td>
	 				
						<a href='javascript:void(0)' onclick='deleteLabfield("${field.id}")'>删除</a>
					</td>
	 			</tr>
	 		</c:forEach>
		</tbody>
	</table> 
</div>
<script>
	var fieldList = new Array(); //新增或者修改的员工列表
	var deletefields = new Array(); //删除的员工列表
	var fieldCount = 0;
	function addField(fieldId){
		
		var url = "expCenterTabController.do?expFieldEdit";
		if(fieldId != undefined && fieldId.length >10){
			url += "&id="+fieldId;
		}
		console.log("url:"+url)
		 $.dialog({
			  	id:'xxx',
				content: 'url:'+url,
				lock : true,
				zIndex: getzIndex(), 
				parent:frameElement.api,
				width:480,
				height:350,
				title:'添加场地', 
				opacity : 0.3,
				cache:false,
			    ok: function(){
			    	iframe = this.iframe.contentWindow;
			    	var field = iframe.getfield();
			    	if(field == null) return false;
			    	handleAddfield(field);
			    },
			    cancelVal: '关闭',
			    cancel: true /*为true等价于function(){}*/
		});
	}
	
	function handleAddfield(field){
		var index = isfieldListExist(field.id);
		if(index != -1){ //更新 替换
			fieldList[index] = field;
			
		}else{ //新增

			if(id.length < 10){id=fieldCount;}
			fieldList[fieldCount] = field;
	 		fieldCount++;
		}
		
		updatefieldTableRow(field); 
		
	}
	
	//修改（新增）员工后，table中更新记录
	function updatefieldTableRow(field){
		var id = field.id;
 		
		if($('#labfield'+field.id).length > 0){
			$('#labfield'+field.id).html('');
		}else{
			$('#fieldTable tbody').append("<tr id='labfield"+id+"'></tr>");
		}
		
		//添加一行
		$('#labfield'+field.id).append("<td>"+field.school+"</td>"+
		"<td>"+field.building+"</td>"+
		"<td>"+field.floor+"</td>"+
		"<td>"+field.room+"</td>"+
		"<td>"+
		"<a href='javascript:void(0)' onclick='deleteLabfield(\""+id+"\")'>删除</a></td>");
	} 
	
	
	function deleteLabfield(id){
		$('#labfield'+id).remove();
		for(var i =0;i<fieldList.length;i++){
			if(fieldList[i] != null && fieldList[i].id == id){
				fieldList[i] = null;
			}
		}
		
		if(id.length > 10){ //非临时id，才保存，传给服务器删除
			deletefields.push(id);
		}
		
	}
	
	var currentModField;
	function modLabfield(id){
		if(id.length < 10){ //id长度小于10位表示是临时id
			currentModField = getfieldFromList(id) ; 
		}else{
			currentModField = {id:id};	
		}
		 
 		
		addField(id);
	}
	
	function getfieldFromList(id){
		for(var i =0;i<fieldList.length;i++){
			if(fieldList[i] != null && fieldList[i].id == id){
				return fieldList[i];
			}
		}
		return null;
	}
	
	/**
	* 判断员工修改新增数组中是否存在此条记录
	*/
	function isfieldListExist(id){
		for(var i =0;i<fieldList.length;i++){
			if(fieldList[i] != null && fieldList[i].id == id){
				return i;
			}
		}
		return -1;
	}
	
</script>